using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

using Module.Common.Models;
using Module.Core.EntitiesInfo;

namespace Module.Core.DAO
{
    public class ProductOrderDAO
	{
		#region Fields

		protected string connectionStringName;

		#endregion
	
		#region Methods

		/// <summary>
		/// Saves a record to the ProductOrder table.
		/// </summary>
		public virtual int Insert(ProductOrderInfo productOrderInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@MemberId", productOrderInfo.MemberId),
				new SqlParameter("@OrderDate", productOrderInfo.OrderDate),
				new SqlParameter("@CreatedDate", productOrderInfo.CreatedDate),
				new SqlParameter("@TotalPrice", productOrderInfo.TotalPrice),
				new SqlParameter("@Notes", productOrderInfo.Notes),
				new SqlParameter("@Status", productOrderInfo.Status)
			};

			return (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "ProductOrder_Insert", parameters);
		}

		/// <summary>
		/// Updates a record in the ProductOrder table.
		/// </summary>
		public virtual void Update(ProductOrderInfo productOrderInfo)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@OrderId", productOrderInfo.OrderId),
				new SqlParameter("@MemberId", productOrderInfo.MemberId),
				new SqlParameter("@OrderDate", productOrderInfo.OrderDate),
				new SqlParameter("@CreatedDate", productOrderInfo.CreatedDate),
				new SqlParameter("@TotalPrice", productOrderInfo.TotalPrice),
				new SqlParameter("@Notes", productOrderInfo.Notes),
				new SqlParameter("@Status", productOrderInfo.Status)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ProductOrder_Update", parameters);
		}

		/// <summary>
		/// Deletes a record from the ProductOrder table by its primary key.
		/// </summary>
		public virtual void Delete(int orderId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@OrderId", orderId)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "ProductOrder_Delete", parameters);
		}

		/// <summary>
		/// Selects a single record from the ProductOrder table.
		/// </summary>
		public virtual ProductOrderInfo Select(int orderId)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@OrderId", orderId)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ProductOrder_Select", parameters))
			{
				if (dataReader.Read())
				{
					return MakeProductOrderInfo(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects all records from the ProductOrder table.
		/// </summary>
		public CHRTList<ProductOrderInfo> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "ProductOrder_SelectAll"))
			{
				CHRTList<ProductOrderInfo> productOrderInfoList = new CHRTList<ProductOrderInfo>();
				while (dataReader.Read())
				{
					ProductOrderInfo productOrderInfo = MakeProductOrderInfo(dataReader);
					productOrderInfoList.Add(productOrderInfo);
				}

				return productOrderInfoList;
			}
		}

		/// <summary>
		/// Creates a new instance of the ProductOrder class and populates it with data from the specified SqlDataReader.
		/// </summary>
		protected virtual ProductOrderInfo MakeProductOrderInfo(SqlDataReader dataReader)
		{
			ProductOrderInfo productOrderInfo = new ProductOrderInfo();
			productOrderInfo.OrderId = SqlClientUtility.GetInt32(dataReader,DbConstants.PRODUCTORDER.ORDERID, 0);
			productOrderInfo.MemberId = SqlClientUtility.GetInt32(dataReader,DbConstants.PRODUCTORDER.MEMBERID, 0);
			productOrderInfo.OrderDate = SqlClientUtility.GetDateTime(dataReader,DbConstants.PRODUCTORDER.ORDERDATE, DateTime.Now);
			productOrderInfo.CreatedDate = SqlClientUtility.GetDateTime(dataReader,DbConstants.PRODUCTORDER.CREATEDDATE, DateTime.Now);
			productOrderInfo.TotalPrice = SqlClientUtility.GetDecimal(dataReader,DbConstants.PRODUCTORDER.TOTALPRICE, Decimal.Zero);
			productOrderInfo.Notes = SqlClientUtility.GetString(dataReader,DbConstants.PRODUCTORDER.NOTES, String.Empty);
			productOrderInfo.Status = SqlClientUtility.GetInt32(dataReader,DbConstants.PRODUCTORDER.STATUS, 0);

			return productOrderInfo;
		}

		#endregion
	}
}
